/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.ruralLibrary.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.ruralLibrary.api.entity.CarouselMapEntity;
import com.pig4cloud.pigx.ruralLibrary.service.CarouselMapService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 轮播图
 *
 * @author pig
 * @date 2023-10-18 14:22:46
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/carouselMap")
@Tag(description = "carouselMap", name = "轮播图管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CarouselMapController {

    private final CarouselMapService carouselMapService;

    /**
     * 分页查询
     *
     * @param page        分页对象
     * @param carouselMap 轮播图
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('ruralLibrary_carouselMap_view')")
    public R getcarouselMapPage(Page page, CarouselMapEntity carouselMap) {
        LambdaQueryWrapper<CarouselMapEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(carouselMap.getCarouselMapName()), CarouselMapEntity::getCarouselMapName, carouselMap.getCarouselMapName());
        return R.ok(carouselMapService.page(page, wrapper));
    }


    /**
     * 通过id查询轮播图
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('ruralLibrary_carouselMap_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(carouselMapService.getById(id));
    }

    /**
     * 新增轮播图
     *
     * @param carouselMap 轮播图
     * @return R
     */
    @Operation(summary = "新增轮播图", description = "新增轮播图")
    @SysLog("新增轮播图")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('ruralLibrary_carouselMap_add')")
    public R save(@RequestBody CarouselMapEntity carouselMap) {
        return R.ok(carouselMapService.save(carouselMap));
    }

    /**
     * 修改轮播图
     *
     * @param carouselMap 轮播图
     * @return R
     */
    @Operation(summary = "修改轮播图", description = "修改轮播图")
    @SysLog("修改轮播图")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('ruralLibrary_carouselMap_edit')")
    public R updateById(@RequestBody CarouselMapEntity carouselMap) {
        return R.ok(carouselMapService.updateById(carouselMap));
    }

    /**
     * 通过id删除轮播图
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除轮播图", description = "通过id删除轮播图")
    @SysLog("通过id删除轮播图")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('ruralLibrary_carouselMap_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(carouselMapService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param carouselMap 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('ruralLibrary_carouselMap_export')")
    public List<CarouselMapEntity> export(CarouselMapEntity carouselMap) {
        return carouselMapService.list(Wrappers.query(carouselMap));
    }

    /**
     * 上传文件 文件名采用uuid,避免原始文件名中带"-"符号导致下载的时候解析出现异常
     *
     * @param file 资源
     * @return R(/ admin / bucketName / filename)
     */
    @PostMapping(value = "/upload")
    public R upload(@RequestPart("file") MultipartFile file) {
        return carouselMapService.uploadFile(file);
    }

    /**
     * 获取文件
     *
     * @param bucket   桶名称
     * @param fileName 文件空间/名称
     * @param response
     * @return
     */
    @Inner(value = false)
    @GetMapping("/{bucket}/{carouselMap}/{fileName}")
    public void file(@PathVariable String bucket, @PathVariable String fileName, @PathVariable String carouselMap, HttpServletResponse response) {
        carouselMapService.getFile(bucket + "/" + carouselMap, fileName, response);
    }

    @GetMapping(value = "/getList")
    @Inner(value = false)
    public R getList(String type) {
        LambdaQueryWrapper<CarouselMapEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CarouselMapEntity::getType, type);
        wrapper.orderByAsc(CarouselMapEntity::getSort);
        return R.ok(carouselMapService.list(wrapper));
    }


    @Inner(value = false)
    @GetMapping("/getPreviewUrl")
    public String getPreviewUrl(String fileName) {
        return carouselMapService.getPreviewUrl(fileName, "lengleng");
    }
}
